<?php


namespace app\controller\merchant\store\product;

use app\common\model\activity\NewActivityCouponRelevance;
use app\common\repositories\store\coupon\StoreCouponUserRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\user\UserRepository;
use crmeb\basic\BaseController;
use app\common\repositories\article\ArticleCategoryRepository;
use app\common\repositories\activity\NewActivityProductRepository;
use app\common\repositories\activity\NewActivityRepository;
use crmeb\jobs\SendNewActivityPeopleCouponJob;
use think\App;
use app\validate\admin\ActivityValidate;
use think\facade\Db;
use think\facade\Queue;

class NewActivityProduct extends BaseController
{
    /**
     * @var NewActivityProductRepository
     */
    protected $repository;

    /**
     * activity constructor.
     * @param App $app
     * @param NewActivityProductRepository $repository
     */
    public function __construct(App $app,NewActivityProductRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    /**
     * @return mixed
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function getActivityList(NewActivityRepository $repository)
    {
        [$page, $limit] = $this->getPage();

        $where = $this->request->params(['title','date']);

        return app('json')->success($repository->search($where, $page, $limit));
    }


    /**
     * @return mixed
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function getList()
    {
        [$page, $limit] = $this->getPage();

        $where = $this->request->params(['keyword','date','status','new_activity_id']);
        return app('json')->success($this->repository->search($this->request->merId(),$where, $page, $limit));
    }

    /**
     * @param NewActivityRepository $repository
     * @return mixed
     * @author: fupeng
     * @day: 2024/1/31
     *
     */
    public function create(NewActivityRepository $repository)
    {
        $res =  $repository->getNewActivity(['is_del'=>0,'type'=>3]);
        if(is_null($res))
            return app('json')->fail('没有进行中的活动');
        $data = $this->request->params([['product_arr',[]],'new_activity_id']);
        if(count($data['product_arr']) <=0)
            return app('json')->fail('请选择参与的商品');
        $product_arr = $data['product_arr'];
        if($res->new_activity_id != $data['new_activity_id'])
            return app('json')->fail('请选择正确的活动');
       Db::transaction(function () use ($product_arr,$res) {
            $arr_insert =[];
            foreach ($product_arr as $v){
                $arr_insert[]=[
                    'new_activity_id'=> $res->new_activity_id,
                    'mer_id'=>$this->request->merId(),
                    'product_id'=> $v,
                ];
            }
            $this->repository->insertAll($arr_insert);
        });
        return  app('json')->success('添加成功');
    }


    /**
     * @param ProductRepository $repository
     * @return mixed
     * @author: fupeng
     * @day: 2024/1/31
     */
    public function productLst(ProductRepository $repository)
    {
        [$page, $limit] = $this->getPage();
        $type = $this->request->param('type',1);
        $where = $this->request->params(['new_activity_id','cate_id','keyword','status','us_status','product_id',['order','sort'],]);
        $where = array_merge($where,$repository->switchType($type,$this->request->merId(),0));
        return app('json')->success($repository->getList($this->request->merId(),$where, $page, $limit));
    }

    /**
     * @param $id
     * @return mixed
     * @author: fupeng
     * @day: 2024/1/29
     * 删除
     */
    public function delete($id)
    {
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, ['is_del' => 1]);
        return  app('json')->success('删除成功');
    }


}
